package cn.kuick.match.test4;

import java.util.ArrayList;
import java.util.List;

public class Test4 {

	public static class LuomaConvert {
		private int[] nums;
		private int result;

		public LuomaConvert(int[] nums) {
			this.nums = nums;
		}	
		
		public void cal() {
			 List<Integer> holes = new ArrayList<Integer>();
			 
			 for(int i=0; i<this.nums.length;) {
				 i = this.findHole(holes, i);
			 }
			 
			 int total = 0;
			 
			 for(int i=0; i<holes.size(); i++) {
				 total += holes.get(i).intValue();
			 }
			 
			 this.result = total;
		}
		
		public int findHole(List<Integer> holes, int startIndex) {
			
			int max = 0;
			int min = 0;
			
			int tempLen = 0;
			int[] temp = new int[nums.length];
			
			temp[tempLen++] = this.nums[startIndex];
			max = temp[0];
			min = temp[0];;
			
			int i;
			for(i=startIndex + 1; i<this.nums.length; i++) {
				 int num = this.nums[i];
				 
				 if (num<=max) {
					 temp[tempLen++] = num;
					 
					 if (num >= max) {
						 max = num;
					 }
					 
					 if (num <= min) {
						 min = num;
					 }
				 } else {
					 break;
				 }
			 }
			 
			 if (tempLen >= 3) {
				 int val = calYuSHui(temp, tempLen, max, min);
				 holes.add(val);
			 }
			 
			 return i;
		}
		
		public int calYuSHui(int[] temp, int len, int max, int min) {
			 int total = 0;
			 
			 for(int i=0; i<len; i++) {
				 int num = temp[i];
				 
				 if (num < max && num >= min) {
					 total += (max - num);
				 }
			 }
			 
			 return total;
		}
		
		public void print() {
			System.out.println(result);
		}
	}
	
	public static void convert(int[] nums) {
		LuomaConvert convert = new LuomaConvert(nums);
		convert.cal();
		convert.print();
	}
	
	public static void main(String[] args) {
		int[] input = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
		convert(input);
	}
}
